package top.whysu.gps.service.auth.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.feilong.lib.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.whysu.gps.constant.CodeConstant;
import top.whysu.gps.dto.auth.ClientCheckDTO;
import top.whysu.gps.exception.BusinessException;
import top.whysu.gps.service.auth.AuthService;
import top.whysu.gps.utils.ComputerInfoUtil;

import java.io.IOException;
import java.util.List;

/**
 * 授权
 */
@Service("AuthServiceImpl")
public class AuthServiceImpl implements AuthService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthServiceImpl.class);

    @Override
    public boolean clientCheck(String clientMac) {
        boolean haveAuth = false;
        if (StringUtils.isBlank(clientMac)) {
            LOGGER.error("客户端MAC地址不能为空");
        }
        // 查询当前服务器所在的电脑的mac地址
        List<String> serverMacList = null;
        try {
            serverMacList = ComputerInfoUtil.getMacAddressList();
        } catch (IOException e) {
            LOGGER.error("获取服务器MAC地址异常");
        }
        // 判断客户端MAC地址是否和服务器MAC地址一致，如果一致的话，可以访问
        if (CollectionUtil.isNotEmpty(serverMacList)) {
            for (String mac : serverMacList) {
                if (mac.equals(clientMac)) {
                    return true;
                }
            }
        }
        // TODO 如果服务端和客户端不在一台电脑，则要查询数据库，查看客户端是否被授权使用



        return haveAuth;
    }
}